package ghr.day0109.travelt.dao.impl;


import ghr.day0109.travelt.dao.RelationDao;
import ghr.day0109.travelt.entity.TourScenicRelation;
import ghr.day0109.travelt.utils.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RelationDaoImpl extends BaseDao implements RelationDao {
    @Override
    public List<TourScenicRelation> queryOneRelation(String id) {
        List<TourScenicRelation> relationList = new ArrayList<>();
        TourScenicRelation tourScenic  = null;
        String sql = "select id,ss_id,tr_id from tour_scenic_relation \n" +
                "where tr_id = ? ";
        super.executeQuery(sql,new Object[]{id});
        try{while(rs.next()){
            tourScenic = new TourScenicRelation();
            tourScenic.setId(rs.getInt("id"));
            tourScenic.setSsId(rs.getString("ss_id"));
            tourScenic.setTrId(rs.getString("tr_id"));
            relationList.add(tourScenic);
        }
        }catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs,pstmt,conn);
        }
        return relationList;
    }

    @Override
    public int addRelation(TourScenicRelation relation) {
        String sql ="insert into tour_scenic_relation(id,ss_id,tr_id) values \n" +
                "(relation_id_seq.nextval,?,?)";
        return super.executeUpdate(sql,new Object[]{relation.getSsId(),relation.getTrId()});
    }

    @Override
    public int deleteRelation(TourScenicRelation relation) {
        String sql =" delete from tour_scenic_relation where tr_id=?";
        return super.executeUpdate(sql,new Object[]{relation.getTrId()});
    }
}
